using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace AdventureWorks.Entities
{
	public class Address : Entity
	{
		[Column("AddressID")]
		public override Int32 ID { get; set; }

		[Display(Name="Address Line 1")]
		[Required]
		[MaxLength(60)]
		public String AddressLine1 { get; set; }

		[Display(Name = "Address Line 2")]
		[MaxLength(60)]
		public String AddressLine2 { get; set; }

		[MaxLength(30)]
		public String City { get; set; }

		[Display(Name = "State or Province")]
		[MaxLength(50)]
		public String StateProvince { get; set; }

		[Display(Name = "Country")]
		[MaxLength(50)]
		public String CountryRegion { get; set; }

		[Display(Name = "Postal Code")]
		[MaxLength(50)]
		public String PostalCode { get; set; }

		[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
		[Display(Name = "Modified Date")]
		public override DateTime ModifiedDate { get; set; }

		[NotMapped]
		[Display(Name="Address")]
		public String AddressLines
		{
			get
			{
				return String.Format("{0} {1}", AddressLine1, AddressLine2);
			}
			private set { }
		}

		public virtual ICollection<Customer> Customers { get; set; }

		public override Boolean Validate()
		{
			if (String.IsNullOrEmpty(AddressLine1))
				ThrowInvalidEntityException(ExceptionMessages.AddressLineInvalid);

			if (ModifiedDate == DateTime.MinValue)
				ModifiedDate = DateTime.Now;

			return true;
		}

		public override string ToString()
		{
			return AddressLines;
		}
	}
}

